package com.zxb.base.soc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxb.base.soc.service.ReplyService;
import com.zxb.base.soc.vo.ObjVo;
import com.zxb.base.soc.vo.ReplyVo;
import com.zxb.core.common.R;
import com.zxb.core.dao.soc.SocReply;
import com.zxb.core.dao.soc.SocReplyDao;
import com.zxb.core.dao.soc.SocTopic;
import com.zxb.core.dao.soc.SocTopicDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Zxb
 */
@RestController
@AllArgsConstructor
@Api(tags = {"留言回复功能"})
public class ReplyController {

    private SocTopicDao topicDao;
    private SocReplyDao replyDao;
    private ReplyService replyService;

    @PostMapping("/reply/topic")
    @ApiOperation("查询评论主题")
    public R<?> topic(@RequestBody @Valid ObjVo objVo) {
        String objType = objVo.getObjType();
        String objKey = objVo.getObjKey();
        SocTopic topic = topicDao.getOne(
                new LambdaQueryWrapper<SocTopic>()
                        .eq(SocTopic::getObjType, objType)
                        .eq(SocTopic::getObjKey, objKey)
                        .orderByAsc(SocTopic::getId));
        if (topic == null) {
            topic = new SocTopic();
            topic.setReplyTimes(0);
            topic.setObjType(objType);
            topic.setObjKey(objKey);
            topicDao.save(topic);
        }
        return R.success(topic);
    }


    @GetMapping("/reply/first/page")
    @ApiOperation("查询第一层评论")
    public R<?> firstPage(Long topicId, Integer page, Integer pageSize) {
        IPage<SocReply> pb = replyDao.page(
                new Page<>(page,pageSize),
                new LambdaQueryWrapper<SocReply>()
                        .eq(SocReply::getTopicId, topicId)
                        .eq(SocReply::getLayer, 1)
                        .orderByDesc(SocReply::getPraiseTimes)
                        .orderByDesc(SocReply::getCreateTime));
        return R.success(pb);
    }

    @GetMapping("/reply/next/page")
    @ApiOperation("查询下一层评论")
    public R<?> nextPage(Long replyId, Integer page, Integer pageSize) {
        IPage<SocReply> pb = replyDao.page(
                new Page<>(page,pageSize),
                new LambdaQueryWrapper<SocReply>()
                        .eq(SocReply::getPid, replyId)
                        .orderByDesc(SocReply::getPraiseTimes)
                        .orderByDesc(SocReply::getCreateTime));
        return R.success(pb);
    }

    @PostMapping("/reply/first/add")
    @ApiOperation("添加第一层评论")
    public R<?> firstAdd(@RequestBody @Valid ReplyVo replyVo) {
        Long replyId = replyVo.getReplyId();
        String content = replyVo.getContent();
        return replyService.addFirst(replyId, content);
    }

    @PostMapping("/reply/next/add")
    @ApiOperation("添加下一层评论")
    public R<?> nextAdd(@RequestBody @Valid ReplyVo replyVo) {
        Long replyId = replyVo.getReplyId();
        String content = replyVo.getContent();
        return replyService.addNext(replyId, content);
    }
}
